import { Input, Select } from 'antd';
import type { FC } from 'react';
import { useEffect, useMemo, useState } from 'react';
import CInput from '../prefix-input';
import CDate from '../prefix-date';
import styles from './index.less';
import type moment from 'moment';
import classnames from 'classnames';
type ValueType = {
  price?: string | number;
  count?: string;
  date?: [moment.Moment, moment.Moment];
};
interface Props {
  value?: ValueType;
  disabled?: boolean;
  onChange?: (value: ValueType) => void;
}
const Index: FC<Props> = ({ onChange, value, disabled }) => {
  const [val, setVal] = useState<ValueType>({
    price: undefined,
    count: undefined,
    date: undefined,
  });
  const update = (value) => {
    if (onChange) {
      onChange?.(value);
    } else {
      setVal(value);
    }
  };
  const onPriceChange = (evt) => {
    update({
      ...val,
      price: evt.target.value?.replace(/^(0+)|[^\d]+/g, ''),
    });
  };
  const onCountChange = (evt) => {
    update({
      ...val,
      count: evt.target.value?.replace(/^(0+)|[^\d]+/g, ''),
    });
  };
  const onDateChange = (date) => {
    update({
      ...val,
      date,
    });
  };
  useEffect(() => {
    if (value) {
      setVal(value);
    } else {
      setVal({
        price: undefined,
        count: undefined,
        date: undefined,
      });
    }
  }, [value]);
  return (
    <div className={classnames(styles.container, { [styles.opacity3]: disabled })}>
      <CInput
        className={styles.margin}
        value={val.price}
        onChange={onPriceChange}
        addonAfter="¥"
        disabled={disabled}
        prefixText="秒杀价"
      />
      <CInput
        className={styles.margin}
        value={val.count}
        onChange={onCountChange}
        disabled={disabled}
        prefixText="限量数"
      />
      <CDate
        value={val.date}
        onChange={onDateChange}
        disabled={disabled}
        prefixText="秒杀时间段"
        showTime
        placeholder={['秒杀开始', '秒杀结束']}
      />
    </div>
  );
};

export default Index;
